package util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.parkcar.R;

import Modle.BaseResult;
import http.IHttpClinetListener;

/**
 * Created by Administrator on 2016/1/9.
 */
public abstract class BaseActivity extends FragmentActivity implements IHttpClinetListener {
    public FrameLayout contentLayout;
    private RelativeLayout load_or_ero;
    private TextView title_name, right_text, left_tv;
    private ImageButton left_iv, right_iv;
    private RelativeLayout title_layout;
    private LoginBroadcast mLoginRecever;
    public  String action;
    public  BaseResult mBaseResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setUpui(savedInstanceState);
        mLoginRecever = new LoginBroadcast();
        registerReceiver(mLoginRecever,new IntentFilter("login"));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mLoginRecever);
    }

    protected abstract void setUpui(Bundle arg0);

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.activity_base);
        contentLayout = (FrameLayout) findViewById(R.id.base_content);
        load_or_ero = (RelativeLayout) findViewById(R.id.load_and_ero);
        title_layout = (RelativeLayout)findViewById(R.id.title_ll);
        initTitle(title_layout);
        View.inflate(this, layoutResID, contentLayout);
    }

    private void initTitle(View view) {
        title_name = (TextView) view.findViewById(R.id.title_name);
        right_text = (TextView) view.findViewById(R.id.right_text);
        left_iv = (ImageButton) view.findViewById(R.id.left_iv);
        right_iv = (ImageButton) view.findViewById(R.id.right_iv);
        left_tv = (TextView) view.findViewById(R.id.left_tv);

        left_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        int top = getResources()
                .getDimensionPixelOffset(R.dimen.header_height);
        ((FrameLayout.LayoutParams) contentLayout.getLayoutParams()).topMargin = top;
    }
    public void setTitleGone(int visibility){
        title_layout.setVisibility(visibility);
        if (visibility != View.GONE) {
            int top = getResources().getDimensionPixelOffset(
                    R.dimen.header_height);
            ((FrameLayout.LayoutParams) contentLayout.getLayoutParams()).topMargin = top;
        } else {
            ((FrameLayout.LayoutParams) contentLayout.getLayoutParams()).topMargin = 0;
        }
    }
    public void setTitlebg(){
        title_layout.setBackgroundResource(R.color.white);
    }
    public void settitleColor(int id){
        title_name.setTextColor(getResources().getColor(id));
    }


    public void setRightImageVisible(int visibleAble) {
        right_iv.setVisibility(visibleAble);
    }

    public void setLeftImage(int resouceId) {
        left_iv.setImageResource(resouceId);
    }

    public void setLeftListener(View.OnClickListener leftListener) {
        left_iv.setOnClickListener(leftListener);
    }
    public void setRight_text(String rightText){
        right_text.setText(rightText);
        if(!BaseTools.isValueEmpty(rightText)){
        right_text.setVisibility(View.VISIBLE);}else{
            right_text.setVisibility(View.GONE);
        }
    }
    public void setRightListener(View.OnClickListener rightListener) {
        right_text.setOnClickListener(rightListener);
        right_iv.setOnClickListener(rightListener);
    }

    public void setTitle(String title) {
        title_name.setText(title);
    }

    public void setTopName(
            String backTitle,
            String titleName) {
        if (!BaseTools.isValueEmpty(backTitle)) {
            left_tv.setVisibility(View.VISIBLE);
            left_tv.setText(backTitle);
        }
        title_name.setText(titleName);
    }

    public void toast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void httpErrListener(String errMsg) {
        toast(errMsg);
    }

    @Override
    public void httpSuccessListener(Bundle successMsg) {
        action = successMsg.getString("action","");
        String dataString  = successMsg.getString("data","");
        if(BaseTools.isValueEmpty(action)){
            httpErrListener("与服务器失去连接，请稍后重试");
            return;
        }
        if(!BaseTools.isValueEmpty(dataString)&&dataString.startsWith("{")&&dataString.endsWith("}")){
            try {
                mBaseResult = JSON.parseObject(dataString, BaseResult.class);
                if(mBaseResult.getStatus().getRetCode()==2){
                    BaseUtil.setToken(this,"");
                    Intent refreshHomeManger = new Intent();
                    refreshHomeManger.setAction("login");
                    sendBroadcast(refreshHomeManger);
                    httpErrListener(mBaseResult.getStatus().getMsg());
                }
            }catch (Exception e){
                Log.e(this.getClass().getName()+"json转化异常",dataString);
            }

        }
    }
    public void showActivity(Class toClass,Bundle mBundle){
        Intent mIntent = new Intent();
        mIntent.setClass(this,toClass);
        if(mBundle!=null){
        mIntent.putExtras(mBundle);
        }
        startActivity(mIntent);
    }
    public void showActivity(Class toClass,Bundle mBundle,int result){
        Intent mIntent = new Intent();
        mIntent.setClass(this,toClass);
        if(mBundle!=null){
            mIntent.putExtras(mBundle);
        }
        startActivityForResult(mIntent,result);
    }
    public void KipActivity(Class toClass,Bundle mBundle){
        showActivity(toClass,mBundle);
        finish();
    }
    class LoginBroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
           boolean login = intent.getBooleanExtra("login",false);
            updateUI(login);
        }
    }
    public void baseBackBottom(View view){
        finish();
    }
     protected abstract void updateUI(boolean isLogin);
}
